package com.dormitoryAdministratorSystem.ui.studentmanage;

import com.dormitoryAdministratorSystem.entity.student.StudentBean;
import com.dormitoryAdministratorSystem.service.student.impl.StudentSerImpl;
import com.dormitoryAdministratorSystem.ui.MainFrame;
import com.dormitoryAdministratorSystem.util.BeansUtil;
import com.dormitoryAdministratorSystem.util.CheckFormatUtil;
import com.lovo.netCRM.component.*;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.sql.Date;
import java.util.*;

public class StudentInDialog extends JDialog {

	private LovoTxt nameTxt;

	private LovoRadioButton genderBtn;

	private LovoComboBox<String> classCmb;// 后期需要把String改为班级Bean

	private LovoComboBox<String> roomCmb;// 后期需要把String改为房间Bean

	private LovoFileChooser headPic;

	private LovoTxt stuTelTxt;

	private LovoDate stuInTime;

	private LovoButton addBtn;

	private LovoButton cancelBtn;

	private StudentSerImpl ssi = new StudentSerImpl();

	public StudentInDialog(MainFrame frame) {
		super(frame, "添加学生", true);
		this.setBounds(350, 250, 500, 400);
		this.setLayout(null);
		this.init();

		this.setVisible(true);
	}

	private void init() {
		// TODO Auto-generated method stub
		this.nameTxt = new LovoTxt("姓名", 20, 40, this);
		this.genderBtn = new LovoRadioButton("性别", new String[] { "男", "女" },
				20, 120, this);

		ArrayList<String> classLst = new ArrayList<String>();
		Set<String> strings = BeansUtil.getcMap().keySet();
		for (String string : strings) {
			classLst.add(string);
		}

		this.classCmb = new LovoComboBox<String>("班级",classLst,20, 200, this);

		ArrayList<String> roomLst = new ArrayList<String>();
		Set<String> strings1 = BeansUtil.getrMap().keySet();
		for (String s : strings1) {
			roomLst.add(s + "(" + BeansUtil.getrMap().get(s).getRoomType().substring(0,1) + ")");
		}

		this.roomCmb = new LovoComboBox<String>( "房间",roomLst, 20, 260, this);

		this.headPic = new LovoFileChooser(this);
		this.headPic.setBounds(320, 20, 100, 150);

		this.stuTelTxt = new LovoTxt("联系电话", 250, 200, this);
		this.stuInTime = new LovoDate("入住时间", 250, 260, this);

		this.addBtn = new LovoButton("添加", 120, 320, this);
		this.cancelBtn = new LovoButton("取消", 270, 320, this);

		this.addBtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub

				try {
					if("".equals(nameTxt.getText().trim()) ||  "".equals(genderBtn.getItem().trim())
						|| "".equals(stuTelTxt.getText().trim())){
						JOptionPane.showMessageDialog(null, "添加失败，信息不能为空");
					}else if(!CheckFormatUtil.isPhoneNum(stuTelTxt.getText())){
						JOptionPane.showMessageDialog(null, "添加失败，电话号码格式错误\n" +
								"必须是13,15,17,18,19开头的11位手机号");
					}else if(!genderBtn.getItem().equals(
							BeansUtil.getrMap().get(roomCmb.getItem().substring(0,roomCmb.getItem().indexOf("(")))
									.getRoomType().substring(0,1))){
						JOptionPane.showMessageDialog(null, "添加失败" +
								"\n男生只能住男生寝室" +
								"\n女生只能住女生寝室");
					}else if(headPic.getFilePath() != null && !headPic.getFilePath()
							.substring(headPic.getFilePath()
								.lastIndexOf(".")).matches("\\.(jpg|png|gif|PNG|JPG|GIF)")){
							JOptionPane.showMessageDialog(null,
									"图片格式错误\n" +
											"格式为: .jpg或.png或.gif");
					}else if(!CheckFormatUtil.checkLength(nameTxt.getText().trim(), 2, 10)){
						JOptionPane.showMessageDialog(null,
								"添加失败，姓名长度范围： 2-10");
						nameTxt.setText("");
					}else{
						StudentBean s = new StudentBean();
						s.setSName(nameTxt.getText().trim());
						s.setGender(genderBtn.getItem());
						s.setPhoneNum(stuTelTxt.getText());
						s.setPhotoPath(savePicture(headPic.getFilePath()));
						s.setSTime(new Date(stuInTime.getDate().getTime()));
						s.setClassBean(BeansUtil.getcMap().get(classCmb.getItem()));
						s.setRoomBean(BeansUtil.getrMap().get(roomCmb.getItem().substring(0,roomCmb.getItem().indexOf("("))));
						ssi.addStuInRoom(s);
						JOptionPane.showMessageDialog(null, "添加成功");
						StudentInDialog.this.dispose();
					}
				}catch (Exception e){
//					e.printStackTrace();
					JOptionPane.showMessageDialog(null,"添加失败，时间格式错误");
				}
			}
		});

		this.cancelBtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				StudentInDialog.this.dispose();
			}
		});
	}

	private String savePicture(String filePath){
		if(filePath != null){
			File file = new File(filePath);
			String newPath = System.currentTimeMillis() + filePath.substring(filePath.lastIndexOf("."));

			File file2 = new File("E:\\IdeaProjects\\dormitory_mamagement\\photo"
					+ File.separator + newPath);

			try (	FileInputStream fis = new FileInputStream(file);
					 FileOutputStream fos = new FileOutputStream(file2)
			){
				int a = 0;
				while(a != -1){
					a = fis.read();
					fos.write(a);
				}
				fos.flush();
			}catch (Exception e){
				e.printStackTrace();
			}
			return newPath;
		}else{
			return null;
		}
	}
}
